/**

 @Name：layuiAdmin 内容系统
 @Author：star1029
 @Site：http://www.layui.com/admin/
 @License：LPPL
    
 */
layui.define(['table', 'form','my_send'], function(exports){
  var $ = layui.$
  ,table = layui.table
  ,form = layui.form
,http_proxy = layui.http_proxy;
  //分类管理
  var tableArray;
  var tableIns = table.render({
    elem: '#LAY-app-content-list'
    ,url:  http_proxy('/materialCaregory/getMcAttrList')//模拟接口
    ,method:'get'
    ,parseData:function(res){
      if(res.rows !=null){
        for (var i = 0; i < res.rows.length; i++) {
          if(res.rows[i].fCaEnabled==1){
              res.rows[i].LAY_CHECKED=true
          }
        }
      }
      console.log(res);
      return{
        "code":0
        ,"count":res.total
        ,"data":res.rows
        ,'msg':''
      }
    }
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'fCaEnabled', width: 100, title: '是否启用',align:'center', templet:'#forfCaEnabled'}
      ,{field: 'fCaShowName', title: '属性显示名称', minWidth: 100,align:'center'}
      ,{field: 'fCaKeyField', title: '是否关键属性',align:'center',templet:'#forfCaKeyField'}
      ,{ title: '排序',align:'center',templet:'#sortButton'}
      ,{title: '操作', minWidth: 150, align: 'center', fixed: 'right', toolbar: '#table-content-list',align:'center'}
    ]]
    ,page: true
    ,limit: 10
    ,id:'LAY-app-content-list'
     ,where:{
       fMcId: jTools.getQuery('fMcId','000')
     }
    ,limits: [10, 15, 20, 25, 30]
    ,text: {
      none:'暂无数据'
    }
    //'对不起，加载出现异常！'
    ,done:function(res,curr,count){
      tableArray = res.data;
    }
  });
  table.on('checkbox(LAY-app-content-list)', function(obj){
    var fMcId;
    var fCaId;  //唯一标志
    var fCaEnabled;
    var checkStatus=table.checkStatus('LAY-app-content-list').data; //表格当前所有勾选的数据
    if(obj.checked ===true && obj.type === 'all'){   //全选
      fMcId=checkStatus[0].fMcId;
      fCaEnabled=1;    //启用
    }else if(obj.checked===true && obj.type==='one'){
      fMcId=obj.data.fMcId;
      fCaId=obj.data.fCaId;   //单独勾选一个
      fCaEnabled=1;     //启用
    }else if(obj.checked===false && obj.type==='one'){
      fMcId=obj.data.fMcId;
      fCaId=obj.data.fCaId;  //单独取消一个
      fCaEnabled=0;   //禁用
    } else if(obj.checked ===false && obj.type === 'all'){   //   取消全部  tableArray---当前页全部数据
      fMcId=tableArray[0].fMcId;
      fCaEnabled=0;    //禁用
    }

    if((obj.checked ===true && obj.type === 'all')||(obj.checked ===false && obj.type === 'all')){
        $.ajax({
            url:http_proxy('/materialCaregory/updateAllCaEnabled'),
            data:{fMcId:fMcId,fCaEnabled:fCaEnabled},
            type:'post',
            success:function (res) {
                $(".layui-laypage-btn").click();
            }
        });
    }else if((obj.checked===true && obj.type==='one')||(obj.checked===false && obj.type==='one')){
        $.ajax({
            url:http_proxy('/materialCaregory/updateCaEnabled'),
            data:{fMcId:fMcId,fCaId:fCaId,fCaEnabled:fCaEnabled},
            type:'post',
            success:function (res) {
              $(".layui-laypage-btn").click();
            }
        });
    }
    // console.log(obj.checked); //当前是否选中状态
    // console.log(obj.data.fCaEnabled); //选中行的相关数据
    // console.log(obj.type); //如果触发的是全选，则为：all，如果触发的是单选，则为：one
    // console.log(table.checkStatus('LAY-app-content-list').data); // 获取表格中选中行的数据
});
  //监听工具条
  table.on('tool(LAY-app-content-list)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      layer.confirm('真的删除此行么？', function(index){
        $.ajax({
          url:http_proxy('/materialCaregory/deleteMcAttr'),
          type:'post',
          data:{
            fCaId:data.fCaId
          },
          success:function(data){
            if(data.code = "success" ){
              obj.del();
              layer.close(index);
              layer.msg(data.message,{icon:6});
              tableIns.reload()
            }else{
              layer.msg(data.message, {icon: 5});
            }
          }
        });
        return false;
      });
    } else if(obj.event === 'edit'){
      layer.open({
        type: 2
        ,title: '编辑分类'
        ,content: '../../views/template/equipment/equipmentform.html?fMcId='+ data.fMcId
        ,maxmin: true
        ,area: ['550px', '550px']
        ,btn: ['确定', '取消']
        ,success: function(layero, index){
          let iframe = window['layui-layer-iframe'+index];
          iframe.initData(data);
          // iframe.initfCaKeyField(data.fCaKeyField);
        }
        ,yes: function(index, layero){
          var iframeWindow = window['layui-layer-iframe'+ index]
          ,submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
          //监听提交
          iframeWindow.layui.form.on('submit(layuiadmin-app-form-edit)', function(data){
            console.log(obj.data.fCaId);
            var field = data.field; //获取提交的字段
            field.fMcId = jTools.getQuery('fMcId');
            field.fCaId = obj.data.fCaId; 
            //提交 Ajax 成功后，静态更新表格中的数据
            $.ajax({
              async: false,
              url:http_proxy('/materialCaregory/saveMcAttr'),
              type:'POST',
              dataType: "json",
              data: field,
              success:function (data) {
                console.log(data);
                  if(data.code = "success"){
                    layer.msg(data.message, {icon: 6});
                    // layer.close(index);
                }else{
                    layer.msg(data.message, {icon: 5});
                }
              }
            });  
            form.render('select');
            //数据更新
            obj.update({
              fCaShowName:field.fCaShowName,
              fCaName:field.fCaName,
              fCaCode:field.fCaCode,
              fCaFieldType:field.fCaFieldType,
              fCaDefaultValue:field.fCaDefaultValue,
              fCaKeyField:field.fCaKeyField,
              fCaLength:field.fCaLength,
              fCaMaxValue:field.fCaMaxValue,
              fCaMinValue:field.fCaMinValue,
              fCaIsNull:field.fCaIsNull,
              fCaIsUniqueness:field.fCaIsUniqueness,
              fCaCompany:field.fCaCompany,
              fMcId:field.fMcId,
              fCaId:field.fCaId,
              fCaOrder:field.fCaOrder,
              fCaEnabled:field.fCaEnabled,
            }); 
            // debugger
            form.render('select');
            table.reload('LAY-app-content-list'); //数据刷新
            layer.close(index); //关闭弹层
          });  
          submit.trigger('click');
        },
      });
    }
    else if(obj.event === 'orderEdita'){
         orderEdit("a",obj.data)
    }else if(obj.event==='orderEditb'){
          orderEdit("b",obj.data)
    }else if(obj.event==='orderEditc'){
          orderEdit("c",obj.data)
    }else if(obj.event==='orderEditd'){
           orderEdit("d",obj.data)
    }
  });
  function orderEdit(orderType,data){
    $.ajax({
      url:http_proxy('/materialCaregory/updateFCaOrder'),
      type:'post',
      data:{
        fCaId:data.fCaId,
        fMcId:data.fMcId,
        fCaOrder:data.fCaOrder,
        optType:orderType
      },
      success:function(data){
        if(data.code = "success" ){
          $(".layui-laypage-btn").click();
          layer.msg(data.message,{icon:6});
       
        }else{
          layer.msg(data.message, {icon: 5});
        }
      }
    });
  }
  exports('contlist', {})
});